/**
 * FileName: EmployeeRoleDaoImpl.java
 * 员工角色Dao实现类
 *
 * @author  王威
 * @Date    2021/11/29
 * @version 1.00
 **/
package com.site.dao.impl;
import com.site.dao.EmployeeRoleDao;
import com.site.pojo.Employee;
import com.site.pojo.EmployeeRole;
import com.site.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeRoleDaoImpl implements EmployeeRoleDao {
    /*查询员工角色*/
    @Override
    public List<EmployeeRole> selectAll() throws SQLException {
        List<EmployeeRole> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT * FROM `employee`");
        while (rst.next()){
            EmployeeRole e = new EmployeeRole();
            e.setEid(rst.getLong("eid"));
            e.setRid(rst.getLong("rid"));
            list.add(e);
        }
        return list;
    }
    /*根据员工id查询全部（员工id/角色编号）*/
    @Override
    public EmployeeRole selectById(EmployeeRole emp) throws SQLException {
        EmployeeRole employeeRole = null;
        Connection con = JDBCUtils.getConnection();
        String sql = "select * from employee_role where eid = ?";
        Object prmas[] = {emp.getEid()};
        ResultSet rst = JDBCUtils.executeQuery(con,sql,prmas);
        while (rst.next()){
            employeeRole = new EmployeeRole();
            employeeRole.setEid(rst.getLong("eid"));
            employeeRole.setRid(rst.getLong("rid"));
        }
        return employeeRole;
    }

    /*添加员工角色*/
    @Override
    public int addEmployeeRole(Connection con, EmployeeRole employeeRole) throws SQLException {
        String sql = "INSERT INTO employee_role(eid,rid)values(?,?)";
        Object[] prmas = {employeeRole.getEid(),employeeRole.getRid()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
    /*删除员工角色*/
    @Override
    public int delEmployeeRole(Connection con, EmployeeRole employeeRole) {
        String sql = "delete from employee_role  where eid  =?";
        return JDBCUtils.executeUpdate(con,sql,new Object[]{employeeRole.getEid()});
    }
    /*更改员工角色*/
    @Override
    public int updateEmployeeRole(Connection con, EmployeeRole employeeRole) {
        String sql = "update ` hurricane_house `.` employee_role ` set` rid ` = ? = where eid = ?};";
        return JDBCUtils.executeUpdate(con,sql,new Object[]{employeeRole.getRid(),employeeRole.getEid()});
    }
}
